package com.stox.dto.preference;

import java.util.HashMap;
import java.util.Map;

public class PreferenceFactory {
	
	private static final Map<String, Preference> preferences = new HashMap<String, Preference>();
	
	@SuppressWarnings("unchecked")
	public static <T extends Preference> T get(Class<T> type){
		Preference preference = preferences.get(type.getCanonicalName());
		if(null == preference){
			try {
				preference = type.newInstance();
				preferences.put(type.getCanonicalName(), preference);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return (T)preference;
	}
	
	public static <T extends Preference> void put(T preference){
		preferences.put(preference.getClass().getCanonicalName(), preference);
	}
	
}
